import Mock from "mockjs";

const accountData = [
    { user: "admin", pwd: "123456", type: "管理员" },
    { user: "a123123", pwd: "123456", type: "用户" }
];


export default {
    // 登录逻辑处理
    login: config => {
        var { user, pwd } = JSON.parse(config.body);
        console.log(user, pwd);
        var res = accountData.find(item => item.user === user);
        if (res) {
            if (res.pwd === pwd) {
                return {
                    code: 200,
                    msg: "登录成功！"
                };
            } else {
                return {
                    code: 401,
                    msg: "密码错误，请重新输入密码！"
                };
            }
        } else {
            return {
                code: 401,
                msg: "该账号不存在！"
            }
        }
    },
    // 注册逻辑处理
    register: config => {
        var { user, pwd } = JSON.parse(config.body);
        console.log(user, pwd);
        var res = accountData.find(item => item.user === user);
        if (res) {
            return {
                code: 401,
                msg: "该账号已存在！"
            }
        } else {
            accountData.push({ user, pwd, type: "用户" });
            return {
                code: 200,
                msg: '注册成功！'
            }
        }

    },
    // 获取用户信息
    getUserInfo: config => {
        var { user } = JSON.parse(config.body);
        var res = accountData.find(item => item.user === user);
        return {
            code: 200,
            msg: "请求成功!",
            type: res.type
        }
    }
}